﻿#if NETFX
using Color = System.Windows.Media.Color;
#elif NETFX_CORE || WINDOWS_PHONE
using Color = Windows.UI.Color;
#endif

using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Text.RegularExpressions;
using NicoView.Model.Service;
using NicoView.Model.Extentions;
using System.Windows;

using System.Windows.Input;
using NicoView.Model.Service.Comments;
using GalaSoft.MvvmLight.Command;
using NicoView.ViewModel.Message;



namespace NicoView.ViewModel
{


    public sealed class NicoViewCommentViewModel:NicoViewViewModelBase,IDisposable
    {


        internal NicoViewCommentViewModel(NotifyNicoComment comment,NicoViewCommentListViewModelBase viewModel)
        {
            Comment = comment;
            _sortTime = DateTime.Now;
            backGroundColorChangeCommand = null;
            this.viewModel = viewModel;
            this.Comment.PropertyChanged += _onPropertyChanged;

            
        }





        private NicoViewCommentListViewModelBase viewModel;

        private NotifyNicoComment comment;

        private void raisePropertyChangedAll()
        {
            this.RaisePropertyChangedAll();

           
        }

        public NotifyNicoComment Comment
        {
            get
            {
                return this.comment;
            }
            set
            {
                this.comment = value;
                this.comment.PropertyChanged += _commentPropertyChanged;
            }
        }

        private void _commentPropertyChanged(object sender, PropertyChangedEventArgs e)
        {


            raisePropertyChangedAll();
            
        }

        public bool IsOwner
        {
            get
            {
                return this.UserType == CommentUserType.Owner;
            }
        }

        public bool IsManager
        {
            get
            {
                return this.UserType == CommentUserType.Manager;
            }
        }



        public void UrlShow(NicoCommentUrlShowMessage e)
        {

            this.viewModel.UrlShow(e);
        }

        public void SingleNoAnchor(NicoCommentSingleNoAnchorMessage e)
        {
            this.viewModel.SingleNoAnchor(e);
        }

        public void MultiNoAnchor(NicoCommentMultiNoAnchorMessage e)
        {
            this.viewModel.MultiNoAnchor(e);
        }

        public void VideoShow(NicoCommentVideoShowMessage e)
        {
            this.viewModel.VideoShow(e);
        }

        public void ChannelShow(NicoCommentChannelShowMessage e)
        {
            this.viewModel.ChannelShow(e);
        }

        public void LiveShow(NicoCommentLiveShowMessage e)
        {
            this.viewModel.LiveShow(e);
        }

        public void CommunityShow(NicoCommentCommunityShowMessage e)
        {
            this.viewModel.CommunityShow(e);
        }

        private DateTime _sortTime;

        public bool IsAnonymous
        {
            get
            {
                return this.Comment.IsAnonymous;
            }
        }

        public CommentBodyType BodyType
        {
            get
            {
                return this.Comment.BodyType;
            }
        }

        
        public bool VisibleComment
        {
            get
            {
                return (!((!this.InternalApplication.Property.VisibleAnonymous && this.Comment.IsAnonymous) ||
                    (!this.InternalApplication.Property.VisibleCommentArtComment && this.Comment.BodyType == CommentBodyType.CommentArt))
                    ) && !this.Comment.Body.IsNoneShowCommand;

            }
        }
        
        public int Score
        {
            get
            {
                return this.comment.Score;
            }
        }

        public CommentScoreRank CommentScoreRank
        {
            get
            {
                return this.comment.CommentScoreRank;
            }
        }


        
        public string ImagePath
        {
            get
            {
                
                return this.Comment.UserIconPath;

            }
        }
        

        
        public string DisplayTime
        {
            get
            {
                
                return this.Comment.Elapsed.ToString();
                
            }
        }
        

        public int No
        {
            get
            {
                return Comment.No;
            }
        }

        public DateTime SortTime
        {
            get
            {
                return Comment.Date;
            }
        }

        public DateTime SecondSortTime
        {
            get
            {
                return _sortTime;
            }
        }


        private ICommand backGroundColorChangeCommand;

        public ICommand BackGroundColorChangeCommand
        {
            get
            {
                if (this.backGroundColorChangeCommand == null) this.backGroundColorChangeCommand = new RelayCommand(() =>
                {
                    if (this.Comment.User.BackGroundColor == NamedColor.NoneColor)
                    {


                        this.Comment.User.BackGroundColor = NotificationNicoUser.GetRundomColor();

                    }
                    else
                    {
                        this.Comment.User.BackGroundColor = NamedColor.NoneColor;
                    }
                });

                return this.backGroundColorChangeCommand;
            }
        }





        public new void Dispose()
        {
            base.Dispose();
            this.Comment.PropertyChanged -= _commentPropertyChanged;
            this.Comment.PropertyChanged -= this._onPropertyChanged;
            backGroundColorChangeCommand = null;
        }


        private void _onPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "No":

                    RaisePropertyChanged(()=>this.DisplayNo);
                    break;
                case "Body":
                    RaisePropertyChanged(() => this.DisplayBody);
                    break;
                case "UserIconPath":
                    RaisePropertyChanged(() => this.ImagePath);
                    break;
                case "NickName":
                case "CustomName":
                    RaisePropertyChanged(() => this.DisplayName);
                    break;
                case "Id":
                    RaisePropertyChanged(() => this.DisplayId);
                    break;
                case "BackGroundColor":
                    RaisePropertyChanged(() => this.BackGroundColor);
                    break;

                case "ElapsedTime":
                case "Time":
                    RaisePropertyChanged(() => this.DisplayTime);
                    break;



            }
        }

        public string DisplayNo
        {
            get { return this.Comment.No != 0 ? this.Comment.No.ToString() : ""; }
        }

        public string DisplayName
        {
            get
            {

                return (this.Comment.User.HasBeenSetCustomName ? "@" : "") + this.Comment.GetDisplayName();

            }
        }

        public string DisplayId
        {
            get { return this.Comment.Id; }
        }

        public NicoCommentBody DisplayBody
        {
            
            get { return this.Comment.Body; }
        }

        public CommentUserType UserType
        {
            get
            {
                return this.Comment.UserType;
            }
        }

        



        
        public Color BackGroundColor
        {
            get
            {
                
               return this.Comment.BackGroundColor;
                
            }
        }
        
        private ICommand _replyRequestCommand;

        public ICommand ReplyRequestCommand
        {
            get
            {
                if (_replyRequestCommand == null) _replyRequestCommand = new RelayCommand(() =>
                {
                    ReplyRequest(new NicoCommentReplyRequestMessage(this));
                });

                return _replyRequestCommand;
            }
        }

        private ICommand _showSingleUserCommand;

        internal void ReplyRequest(NicoCommentReplyRequestMessage e)
        {
            this.viewModel.ReplyRequest(e);
        }

        public ICommand ShowSingleUserCommand
        {
            get
            {
                if (_showSingleUserCommand == null) _showSingleUserCommand = new RelayCommand(() =>
                {
                    this.ShowSingleUser(new NicoViewShowSingleUserInfoMessage(this.Comment.User,this.BaseViewModel.ApplicationDispatcher));

                });

                return _showSingleUserCommand;
            }
        }

        public NicoViewViewModelBase BaseViewModel
        {
            get
            {
                return this.viewModel;
            }
        }

        public void ShowSingleUser(NicoViewShowSingleUserInfoMessage e)
        {
            this.viewModel.ShowSingleUser(e);
        }
    }
}
